﻿/////////////////////////////////////////////////////////////////////
//
//	Map_FVG C# Class Library.
//
//	ReadFileX
//	
//
//	autore: Gustavo Mainardi 
//	versione: 1.0
//	data: 01/12/2015
//
//
//	Cronologia delle versioni:
//
//	Versione 1.0 01/12/2015
//		versione originale
//
//////////////////////////////////////////////////////////////////////////
using System;
using System.Windows.Forms;
using System.Drawing;
using System.IO;
using System.Globalization;
using System.Data.SQLite;

namespace Map_Fvg
{
    public partial class ViewQ
    {
        static private string coordVertices;

        static public int readFileLine(string path)
        {


            int ipos = FormIniz.nameSect.LastIndexOf('.');
            if (ipos >= 0)
            {
                selectedElement = FormIniz.nameSect.Substring(0, 6);
                FormIniz.nameElement = FormIniz.nameSect.Substring(0, ipos);
            }

            string FileName = path + "line_dbf.txt";
            StreamReader sf = File.OpenText(FileName);
            string s = sf.ReadLine();
            if (!s.Contains("numero records:"))
            {
                MessageBox.Show("file " + path + " non conforme");
                return 1;
            }

            ipos = s.IndexOf(':');
            ++ipos;
            int length = s.Length;

            string ss = s.Substring(ipos, length - ipos);
            int nrec = int.Parse(ss);
            max_nShapes = nrec + 100;   //      spazio per inserimento nuove linee

            vectShapes = new vShapesL[max_nShapes];

            char[] delimiterChars2 = { '#' };
            string[] words;
            int i, ii = 0, ipn = 0;
            for (i = 0; i < nrec; ++i)
            {
                s = sf.ReadLine();
                words = s.Split(delimiterChars2);   
                vectS[ipn].codice = words[1].Trim();
                vectS[ipn].denom = words[2].Trim();
                vectS[ipn].classe = words[3].Trim();
                vectS[ipn].elemento = words[4].Trim();


                if (i == 0)
                {
                    ++ipn;
                    goto L40;
                }
                for (ii = 0; ii < ipn; ++ii)
                {
                    int icomp = string.Compare(vectS[ii].codice, 0, vectS[ipn].codice, 0, 8);
                    int icomp1 = string.Compare(vectS[ii].elemento, 0, vectS[ipn].elemento, 0, 8);
                    if (icomp == 0 && icomp1 == 0) goto L40;
                }
                ++ipn;
            L40:
                if (i == 0)
                {
                    vectShapes[i].pntSimb = 0;
                }
                else
                {
                    vectShapes[i].pntSimb = ii;
                }

                string numID = words[0].Trim();
                vectShapes[i].numID = int.Parse(numID);

                string elem = words[4].Trim();
                int comp = string.Compare(elem, selectedElement);
                if (comp == 0) vectShapes[i].selected = 1;
                else vectShapes[i].selected = 0;

                vectShapes[i].edizione = words[5];

                if (words.Length > 7)
                {
                    vectShapes[i].lunghezza = words[6].Trim();
                    vectShapes[i].quota = words[7].Trim();
                }
                else
                {
                    vectShapes[i].lunghezza = "0.0";
                    vectShapes[i].quota = "0.0";
                }

                vectShapes[i].tipo = 0;
                if(vectS[ii].codice.Contains("1L000II")) vectShapes[i].tipo = 1;    //curve di livello intermedie
                if(vectS[ii].codice.Contains("1L000CI")) vectShapes[i].tipo = 1;
                if (vectS[ii].codice.Contains("1L000CD")) vectShapes[i].tipo = 2;    //curve di livello direttrici
                if(vectS[ii].codice.Contains("1L000DI")) vectShapes[i].tipo = 2;
           }

            int num1 = 0;
            int num2 = 0;
            for(i = 0;i < nrec;++i)
            {
                if(vectShapes[i].tipo == 1) ++ num1;
                if(vectShapes[i].tipo == 2) ++ num2;
            }


            QuotaLivInt = new QuotaLiv[num1];
            numQuotaLivInt = num1;

            int ipt = 0;
            for (i = 0; i < nrec; ++i)
            {
                if (vectShapes[i].tipo == 1)
                {
                    QuotaLivInt[ipt].num = i;
                    double dbl = double.Parse(vectShapes[i].quota);
                    QuotaLivInt[ipt].quota = (int)dbl;   
                    ++ipt;
                }
            }

            QuotaLivDir = new QuotaLiv[num2];
            numQuotaLivDir = num2;

            ipt = 0;
            for (i = 0; i < nrec; ++i)
            {
                if (vectShapes[i].tipo == 2)
                {
                    QuotaLivDir[ipt].num = i;
                    double dbl = double.Parse(vectShapes[i].quota);
                    QuotaLivDir[ipt].quota = (int)dbl;
                    ++ipt;
                }
            } 
           
            
            sf.Close();
            nSimb = ipn;


            if (!FormIniz.bVeneto)
            {
                readQU5000(selectedElement);

                computeRet();

                double Xdiff = vert[1].X - vert[0].X;
                double Ydiff = vert[1].Y - vert[0].Y;
                double alphaRad = Math.Atan2(Xdiff, Ydiff);

                alpha = -(float)(180.0 * alphaRad / Math.PI);
            }
            else
            {
                alpha = 0.0F;
            }
            
            
            
            
            for (i = 0; i < nSimb; ++i)
            {
                vectS[i].penType = 1;
                vectS[i].penWidth = 1;
                vectS[i].icolor = Color.Blue;
                vectS[i].visib = 1;
                vectS[i].tratto = "1 / 0.13";

                int ipt1 = string.Compare(vectS[i].elemento, 0, selectedElement, 0, 6);
                if (ipt1 == 0)
                {
                    vectS[i].selected = 1;
                }
                else
                {
                    vectS[i].selected = 0;
                }
            }

            FileName = path + "\\line_shp.txt";
            sf = File.OpenText(FileName);

            s = sf.ReadLine();
            if (!s.Contains("Shapefile type: ArcZ"))
            {
                MessageBox.Show("file line_shp.txt  non conforme");
                return 1;
            }
            char[] delimiterChars0 = { ':', ';' };

            words = s.Split(delimiterChars0);

            ss = words[2];
            int nrec1 = int.Parse(ss);

            ss = words[4];
            int nPoints = int.Parse(ss);
            max_nPoints = nPoints + 15000;   //  spazio per inserimento nuovi punti

            nShapes = nrec;
            if (nrec != nrec1)
            {
                MessageBox.Show("file shp non congruente con dbf: " + nrec1.ToString() + "/  " + nrec1.ToString());
                return 1;

            }

            PL = new Point[max_nPoints];

            s = sf.ReadLine();

            char[] delimiterChars = { ':', ',' };

            words = s.Split(delimiterChars);

            Xmin = int.Parse(words[1]);
            Ymin = int.Parse(words[2]);
            Xmax = int.Parse(words[3]);
            Ymax = int.Parse(words[4]);

            int idShape, nParts;
            int nVertices, ipnt;
            ipnt = 0;
            int XX, YY;
            int iShapes = 0;
            char[] delimiterChars20 = { ':', ',', '=' };
            for (i = 0; i < nShapes; ++i)
            {
                s = sf.ReadLine();
                words = s.Split(delimiterChars20);

                ss = words[1];
                idShape = int.Parse(ss);

                ss = words[3];
                nVertices = int.Parse(ss);

                ss = words[5];
                nParts = int.Parse(ss);

                vectShapes[i].nVertices = nVertices;
                vectShapes[i].pntpL = ipnt;

                s = sf.ReadLine();

                words = s.Split(delimiterChars20);

                vectShapes[i].PointA.X = int.Parse(words[1]);
                vectShapes[i].PointA.Y = int.Parse(words[2]);
                vectShapes[i].PointB.X = int.Parse(words[3]);
                vectShapes[i].PointB.Y = int.Parse(words[4]);
                vectShapes[i].selected = 0;

                if (nParts != 1)
                {
                    s = sf.ReadLine();
                }

                int pntSimb = vectShapes[i].pntSimb;
                if (vectS[pntSimb].selected == 1 && noElem > 1)
                {
                    if (iShapes == 0)
                    {
                        Xmin = vectShapes[i].PointA.X;
                        Ymin = vectShapes[i].PointA.Y;
                        Xmax = vectShapes[i].PointB.X;
                        Ymax = vectShapes[i].PointB.Y;
                        iShapes = 1;
                    }
                    else
                    {
                        if (vectShapes[i].PointA.X < Xmin)
                            Xmin = vectShapes[i].PointA.X;
                        if (vectShapes[i].PointA.Y < Ymin)
                            Ymin = vectShapes[i].PointA.Y;
                        if (vectShapes[i].PointB.X > Xmax)
                            Xmax = vectShapes[i].PointB.X;
                        if (vectShapes[i].PointB.Y > Ymax)
                            Ymax = vectShapes[i].PointB.Y;
                    }
                    ++iShapes;
                }

                for (int j = 0; j < nVertices; ++j)
                {
                    s = sf.ReadLine();

                    words = s.Split(delimiterChars20);

                    XX = int.Parse(words[0]);

                    YY = int.Parse(words[1]);

                    PL[ipnt].X = XX;
                    PL[ipnt].Y = YY;
                    ++ipnt;
                }

            }

            sf.Close();
            readViewQ = 1;
            return 0;
        }


        static private void readQU5000(string selectedElement)
        {
            string selElem = selectedElement.Substring(0, 6);

            using (SQLiteConnection conn = SetConnection1())
            {
                conn.Open();
                using (SQLiteCommand cmd = new SQLiteCommand(conn))
                {
                    string ss1 = "select * from QU_5000 WHERE codice = '";
                    ss1 = string.Concat(ss1, selElem, "'");

                    cmd.CommandText = ss1;
// tables:  QU_5000   QU_10000  QU_25000
//  campi:  codice nome  X1  Y1  X2  Y2  X3  Y3  X4  Y4

                    string codice = string.Empty;
                    coordVertices = string.Empty;

                    SQLiteDataReader reader = cmd.ExecuteReader();
                    if (reader.HasRows)
                    {
                        while (reader.Read())
                        {
                            codice = reader.GetString(0).ToString();
                            IDtavol = reader.GetString(1).ToString();
                            for (int j = 2; j < 10; ++j)
                            {
                                string ss = reader.GetString(j).ToString();
                                ss = ss.Trim();
                                coordVertices = string.Concat(coordVertices, ss, " ");
                            }
                        }
                    }
                }

                conn.Close();
            }

        }

        public static Point[] retic = new Point[20];

        public static Point[] vert = new Point[4];
        //  primo vertice in basso a sinistra; rotazione oraria

        static private void computeRet()
        {

            string s = coordVertices.Trim();

            int ipos = s.IndexOf(' ');
            string s1 = s.Substring(0, ipos);
            vert[0].X = int.Parse(s1);
            int ncar = ipos + 1;
            s1 = s.Substring(ipos, ncar);
            vert[0].Y = int.Parse(s1);
            ipos += ncar;
            s1 = s.Substring(ipos, ncar);
            vert[1].X = int.Parse(s1);
            ipos += ncar;
            s1 = s.Substring(ipos, ncar);
            vert[1].Y = int.Parse(s1);
            ipos += ncar;
            s1 = s.Substring(ipos, ncar);
            vert[2].X = int.Parse(s1);
            ipos += ncar;
            s1 = s.Substring(ipos, ncar);
            vert[2].Y = int.Parse(s1);
            ipos += ncar;
            s1 = s.Substring(ipos, ncar);
            vert[3].X = int.Parse(s1);
            ipos += ncar;
            s1 = s.Substring(ipos, ncar);
            vert[3].Y = int.Parse(s1);
            int XX = vert[0].X / 500;
            XX = 500 * XX;
            int i = 0;
            for (i = 0; i < 20; ++i)
            {
                XX = XX + 500;
                if (XX > vert[2].X) break;
                retic[i].X = XX;
            }
            retic[i].X = 0;

            int YY = vert[0].Y / 500;
            YY = 500 * YY;
            for (i = 0; i < 20; ++i)
            {
                YY = YY + 500;
                if (YY > vert[1].Y) break;
                retic[i].Y = YY;
            }
            retic[i].Y = 0;

            retic.CopyTo(retic, 0);
            vert.CopyTo(vert, 0);

        }

        static public int readFileText(string path)
        {
            string FileName = path + "\\text_dbf.txt";
            StreamReader sf = File.OpenText(FileName);
            string s = "";
            s = sf.ReadLine();
            if (!s.Contains("numero records:"))
            {
                MessageBox.Show("file " + path + " non conforme");
                return 1;
            }
            int ipos = s.IndexOf(':');
            int length = s.Length;

            string ss = s.Substring(ipos + 1, length - ipos - 1);

            int nrec = int.Parse(ss);
            max_nShapesT = nrec + 100;
            vectShapesT = new vShapesT[max_nShapesT];

            int i, ii = 0, ipn = 0;

            char[] delimiterChars2 = { '#' };
            string[] words;
            NumberFormatInfo nfi = new CultureInfo("en-US", false).NumberFormat;

            for (i = 0; i < nrec; ++i)
            {
                s = sf.ReadLine();
                words = s.Split(delimiterChars2);

                vectT[ipn].codice = words[1].Trim();
                vectT[ipn].denom = words[2].Trim();
                vectT[ipn].classe = words[3].Trim();
                vectT[ipn].elemento = words[4].Trim();
                ss = words[5].Trim();
                vectT[ipn].edizione = int.Parse(ss);
                ss = words[7].Trim();
                vectT[ipn].altezza = float.Parse(ss, nfi);

                if (i == 0)
                {
                    ++ipn;
                    goto L40;
                }
                for (ii = 0; ii < ipn; ++ii)
                {
                    int icomp = string.Compare(vectT[ii].codice, 0, vectT[ipn].codice, 0, 8);
                    int icomp1 = string.Compare(vectT[ii].elemento, 0, vectT[ipn].elemento, 0, 8);
                    if (icomp == 0 && icomp1 == 0) goto L40;
                }
                ++ipn;
            L40:
                if (i == 0)
                {
                    vectShapesT[i].pntSimb = 0;
                }
                else
                {
                    vectShapesT[i].pntSimb = ii;
                }

                string elem = words[4].Trim();
                int comp = string.Compare(elem, selectedElement);
                if (comp == 0) vectShapesT[i].selected = 1;
                else vectShapesT[i].selected = 0;

                vectShapesT[i].testo = words[6].Trim();

                ss = words[7].Trim();

                vectShapesT[i].altezza = float.Parse(ss, nfi);

                ss = words[9].Trim();
                int ncar = int.Parse(ss);
                if (ncar > vectShapesT[i].testo.Length) {    }
                ss = words[8].Trim();
                vectShapesT[i].rotazione = float.Parse(ss, nfi);
                ss = words[9].Trim();
                vectShapesT[i].ncar_testo = int.Parse(ss);
                ss = words[10].Trim();
                vectShapesT[i].cod_posiz = int.Parse(ss);
                ss = words[11].Trim();
                vectShapesT[i].GMrotation = float.Parse(ss, nfi);

                vectShapesT[i].modifica = 0;
            }
            sf.Close();
            nSimbT = ipn;

            ipn = 0;
            for (i = 0; i < nSimbT; ++i)
            {
                vectT[i].penType = 1;
                vectT[i].penWidth = 1;
                vectT[i].icolor = Color.Black;
                vectT[i].iFont = new Font("Microsoft Sans Serif", 6.0F, FontStyle.Regular, GraphicsUnit.Point);
                vectT[i].visib = 1;

                int ipt = string.Compare(vectT[i].elemento, 0, selectedElement, 0, 6);
                if (ipt == 0)
                {
                    vectT[i].selected = 1;
                    ++ipn;
                }
                else
                {
                    vectT[i].selected = 0;
                }
            }

            FileName = path + "\\text_shp.txt";
            sf = File.OpenText(FileName);

            s = sf.ReadLine();
            if (!s.Contains("Shapefile type: MultiPointZ"))
            {
                MessageBox.Show("file text_shp.txt  non conforme");
                return 1;
            }
            char[] delimiterChars0 = { ':', ';' };
            words = s.Split(delimiterChars0);

            ss = words[2];
            //           MessageBox.Show("numero records: " + ss);
            int nrec1 = int.Parse(ss);
            nShapesT = nrec;

            if (nrec != nrec1)
            {
                MessageBox.Show("file shp non congruente con dbf: " + nrec.ToString() + "/  " + nrec1.ToString());
                return 1;

            }
            s = sf.ReadLine();

            int idShape, nParts;
            int nVertices, ipnt;
            ipnt = 0;
            int XX, YY;
            char[] delimiterChars3 = { ':', ',', '=' };

            for (i = 0; i < nShapesT; ++i)
            {
                s = sf.ReadLine();


                words = s.Split(delimiterChars3);

                ss = words[1];
                idShape = int.Parse(ss);

                ss = words[3];
                nVertices = int.Parse(ss);

                ss = words[5];
                nParts = int.Parse(ss);
                s = sf.ReadLine();

                for (int j = 0; j < nVertices; ++j)
                {
                    s = sf.ReadLine();

                    words = s.Split(delimiterChars3);

                    XX = int.Parse(words[0]);

                    YY = int.Parse(words[1]);

                    vectShapesT[ipnt].XX.X = XX;
                    vectShapesT[ipnt].XX.Y = YY;
                    vectShapesT[ipnt].modifica = 0;
                    ++ipnt;
                }

            }

            sf.Close();

            return 0;
        }

        static public int readFilePoint(string path)
        {
            string FileName = path + "\\point_dbf.txt";
            StreamReader sf = File.OpenText(FileName);
            string s = "";
            s = sf.ReadLine();
            if (!s.Contains("numero records:"))
            {
                MessageBox.Show("file " + path + " non conforme");
                return 1;
            }
            int ipos = s.IndexOf(':');
            int length = s.Length;

            string ss = s.Substring(ipos + 1, length - ipos - 1);
            int nrec = int.Parse(ss);
            max_nShapesP = nrec + 100;   //      spazio per inserimento nuovi simboli punti
            vectShapesP = new vShapesP[max_nShapesP];

            int i, ii = 0, ipn = 0;

            char[] delimiterChars = { '#' };
            NumberFormatInfo nfi = new CultureInfo("en-US", false).NumberFormat;

            for (i = 0; i < nrec; ++i)
            {
                s = sf.ReadLine();
                string[] words = s.Split(delimiterChars);

                ss = words[1].Trim();
                vectP[ipn].codice = ss;
                ss = words[2].Trim();
                vectP[ipn].denom = ss;
                ss = words[3].Trim();
                vectP[ipn].classe = ss;
                ss = words[4].Trim();
                vectP[ipn].elemento = ss;
                ss = words[8].Trim();
                vectP[ipn].dimensione = float.Parse(ss, nfi);
                if (i == 0)
                {
                    ++ipn;
                    goto L40;
                }
                for (ii = 0; ii < ipn; ++ii)
                {
                    int icomp = string.Compare(vectP[ii].codice, 0, vectP[ipn].codice, 0, 8);
                    int icomp1 = string.Compare(vectP[ii].elemento, 0, vectP[ipn].elemento, 0, 8);
                    if (icomp == 0 && icomp1 == 0) goto L40;
                }
                ++ipn;
            L40:
                if (i == 0)
                {
                    vectShapesP[i].pntSimb = 0;
                }
                else
                {
                    vectShapesP[i].pntSimb = ii;
                }

                string elem = words[4].Trim();
                int comp = string.Compare(elem, selectedElement);
                if (comp == 0) vectShapesP[i].selected = 1;
                else vectShapesP[i].selected = 0;
                vectShapesP[i].elemento = elem;

                ss = words[5].Trim();
                vectShapesP[i].edizione = int.Parse(ss, nfi);

                ss = words[6].Trim();
                vectShapesP[i].quota = float.Parse(ss, nfi);

                ss = words[7].Trim();
                vectShapesP[i].rotazione = float.Parse(ss, nfi);

                ss = words[8].Trim();
                vectShapesP[i].dimensione = float.Parse(ss, nfi);

                ss = words[9].Trim();
                vectShapesP[i].GMrotation = float.Parse(ss, nfi);
            }
            sf.Close();
            nSimbP = ipn;
            ipn = 0;

            for (i = 0; i < nSimbP; ++i)
            {
                vectP[i].icolor = Color.Black;
                vectP[i].iFont = new Font("Microsoft Sans Serif", 5.0F, FontStyle.Regular, GraphicsUnit.Point);
                vectP[i].visib = 1;
                vectP[i].simbolo = 0;
                vectP[i].paramSimb = " ";
                int ipt = string.Compare(vectP[i].elemento, 0, selectedElement, 0, 6);
                if (ipt == 0)
                {
                    vectP[i].selected = 1;
                    ++ipn;
                }
                else vectP[i].selected = 0;
            }

            {
                FileName = path + "\\point_shp.txt";
                sf = File.OpenText(FileName);

                s = sf.ReadLine();
                if (!s.Contains("Shapefile type: MultiPointZ"))
                {
                    MessageBox.Show("file point_shp.txt  non conforme");
                    return 1;
                }
                char[] delimiterChars0 = { ':', ';' };
                string[] words = s.Split(delimiterChars0);

                ss = words[2];
                //           MessageBox.Show("numero records: " + ss);
                int nrec1 = int.Parse(ss);
                nShapesP = nrec;
                if (nrec != nrec1)
                {
                    MessageBox.Show("file shp non congruente con dbf: " + nrec1.ToString() + "/  " + nrec1.ToString());
                    return 1;

                }
                s = sf.ReadLine();

                int idShape, nParts;
                int nVertices, ipnt;
                ipnt = 0;
                int XX, YY;

                for (i = 0; i < nShapesP; ++i)
                {
                    s = sf.ReadLine();

                    char[] delimiterChars2 = { ':', ',', '=' };

                    words = s.Split(delimiterChars2);

                    ss = words[1];
                    idShape = int.Parse(ss);

                    ss = words[3];
                    nVertices = int.Parse(ss);

                    ss = words[5];
                    nParts = int.Parse(ss);
 
                    s = sf.ReadLine();

                    for (int j = 0; j < nVertices; ++j)
                    {
                        s = sf.ReadLine();

                        words = s.Split(delimiterChars2);

                        XX = int.Parse(words[0]);

                        YY = int.Parse(words[1]);

                        vectShapesP[ipnt].XX.X = XX;
                        vectShapesP[ipnt].XX.Y = YY;
                        ++ipnt;
                    }

                }
            }
            sf.Close();
            return 0;
        }

        static public int readFileArea(string path)
        {
            string FileName = path + "\\area_dbf.txt";
            StreamReader sf = File.OpenText(FileName);
            string s = "";
            s = sf.ReadLine();
            if (!s.Contains("numero records:"))
            {
                MessageBox.Show("file " + path + " non conforme");
                return 1;
            }
            int ipos = s.IndexOf(':');
            ++ipos;
            int length = s.Length;

            string ss = s.Substring(ipos, length - ipos);
            int nrec = int.Parse(ss);
            vectShapesA = new vShapesA[nrec];

            int i, ii = 0, ipn = 0;

            char[] delimiterChars = { '#' };
            for (i = 0; i < nrec; ++i)
            {
                s = sf.ReadLine();
                string[] words = s.Split(delimiterChars);

                ss = words[1].Trim();
                vectA[ipn].codice = ss;
                ss = words[2].Trim();
                vectA[ipn].denom = ss;
                ss = words[3].Trim();
                vectA[ipn].classe = ss;
                ss = words[4].Trim();
                vectA[ipn].elemento = ss;
                vectShapesA[i].elemento = ss;
                int cmp = string.Compare(vectShapesA[i].elemento, 0, selectedElement, 0, 6);
                if (cmp == 0)               {
                    vectShapesA[i].selected = 1;
                }
                else vectShapesA[i].selected = 0;

                if (i == 0)
                {
                    ++ipn;
                    goto L40;
                }
                for (ii = 0; ii < ipn; ++ii)
                {
                    int icomp = string.Compare(vectA[ii].codice, 0, vectA[ipn].codice, 0, 8);
                    if (icomp == 0 ) goto L40;
                }
                ++ipn;
            L40:
                if (i == 0)
                {
                    vectShapesA[i].pntSimb = 0;
                }
                else
                {
                    vectShapesA[i].pntSimb = ii;
                }
                ss = words[0].Trim();
                vectShapesA[i].identif = ss;

            }

            sf.Close();
            nSimbA = ipn;

            ipn = 0;
            for (i = 0; i < nSimbA; ++i)
            {
                vectA[i].penType = 1;
                vectA[i].penWidth = 1;
                vectA[i].icolor = Color.Black;
                vectA[i].iFont = new Font("Microsoft Sans Serif", 8.25F, FontStyle.Regular, GraphicsUnit.Point);
                vectA[i].visib = 1;
                vectA[i].codice_bordo = 1;
                vectA[i].bordo = " 1 / 0.13";
                vectA[i].codice_camp = 0;
                vectA[i].campitura = " ";

                int ipt = string.Compare(vectA[i].elemento, 0, selectedElement, 0, 6);
                if (ipt == 0)
                {
                    vectA[i].selected = 1;
                    ++ipn;
                }
                else vectA[i].selected = 0;
            }
            {
                FileName = path + "\\area_shp.txt";
                sf = File.OpenText(FileName);
                s = sf.ReadLine();
                if (!s.Contains("Shapefile type: PolygonZ"))
                {
                    MessageBox.Show("file area_shp.txt  non conforme");
                    return 1;
                }
                char[] delimiterChars0 = { ':', ';' };
                string[] words = s.Split(delimiterChars0);

                ss = words[2];
                //           MessageBox.Show("numero records: " + ss);
                int nrec1 = int.Parse(ss);
                ss = words[4];
                //           MessageBox.Show("numero punti: " + ss);
                int nPoints = int.Parse(ss);

                nShapesA = nrec;
                nPointsA = nPoints;

                if (nrec != nrec1)
                {
                    MessageBox.Show("file area_shp non congruente con dbf: " + nrec1.ToString() + "/  " + nrec1.ToString());
                    return 1;

                }
                PA = new Point[nPoints];

                s = sf.ReadLine();

                char[] delimiterChars1 = { ':', ',' };

                words = s.Split(delimiterChars1);

                int idShape, nParts;
                int nVertices, ipnt;
                ipnt = 0;
                int XX, YY;

                char[] delimiterChars2 = { ':', ',', '=' };
                for (i = 0; i < nShapesA; ++i)
                {
                    s = sf.ReadLine();


                    words = s.Split(delimiterChars2);

                    ss = words[1];
                    idShape = int.Parse(ss);

                    ss = words[3];
                    nVertices = int.Parse(ss);

                    ss = words[5];
                    nParts = int.Parse(ss);

                    vectShapesA[i].nVertices = nVertices;
                    vectShapesA[i].pntPA = ipnt;

                    s = sf.ReadLine();

                    words = s.Split(delimiterChars2);

                    vectShapesA[i].PointA.X = int.Parse(words[1]);
                    vectShapesA[i].PointA.Y = int.Parse(words[2]);
                    vectShapesA[i].PointB.X = int.Parse(words[3]);
                    vectShapesA[i].PointB.Y = int.Parse(words[4]);

                    int pntSimb = vectShapesA[i].pntSimb;

                    if (nParts > 1)
                    {
                        s = sf.ReadLine();
                        int ipos1 = s.IndexOf(":",0);
                        ++ipos1;
                        int len = s.Length;
                        vectShapesA[i].nParts = nParts;
                        vectShapesA[i].Parts_string = s.Substring(ipos1, len-ipos1);
                    }
                    else 
                    {
                        vectShapesA[i].nParts = 1;
                        vectShapesA[i].Parts_string = string.Empty;
                    }
                    for (int j = 0; j < nVertices; ++j)
                    {
                        s = sf.ReadLine();
                        ss = s.Substring(4, s.Length - 4);
                        words = ss.Split(delimiterChars2);

                        XX = int.Parse(words[0]);

                        YY = int.Parse(words[1]);

                        PA[ipnt].X = XX;
                        PA[ipnt].Y = YY;
                        ++ipnt;
                    }

                }
            }
            sf.Close();
            return 0;
        }

        private static bool ExistElement(string elem)
        {
            bool bRet = false;
            using (SQLiteConnection conn = SetConnection1())
            {
                conn.Open();
                using (SQLiteCommand cmd = new SQLiteCommand(conn))
                {

                    string ss = "select nome from QU_5000 WHERE codice = '";
                    ss = string.Concat(ss, elem, "'");

                    cmd.CommandText = ss;

                    SQLiteDataReader reader = cmd.ExecuteReader();
                    if (reader.HasRows) bRet = true;
                }

                conn.Close();
            }

            return bRet;
        }

        private static SQLiteConnection SetConnection1()
        {
            string DataSource = "Data Source=";
            DataSource = string.Concat(DataSource, FormIniz.quadroUnione, ";");
            SQLiteConnection conn = new SQLiteConnection(DataSource);
            return (conn);
        }
    }
}

